package com.yiya.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiya.bean.ForgetPwd;
import com.yiya.bean.SysUser;
import com.yiya.mail.MailSenderInfo;
import com.yiya.mail.SimpleMailSender;
import com.yiya.mapper.ForgetPwdMapper;
import com.yiya.mapper.SysUserMapper;
@Service("mailService")
public class MailService {
	private static ResourceBundle mailRes = ResourceBundle.getBundle("mail");
	private static ResourceBundle urlRes = ResourceBundle.getBundle("urls");
	
	@Autowired
	private SysUserMapper<SysUser> sysUserMapper;
	
	@Autowired
	private ForgetPwdMapper forgetPwdMapper;
	
	/**
	 * 
	 * @param title 邮件标题
	 * @param content 邮件内容
	 * @param targetAddress 目标邮箱地址
	 */
	public void sendMail(String title,String content,String targetAddress){
		  //这个类主要是设置邮件
		  MailSenderInfo mailInfo = new MailSenderInfo(); 
		  mailInfo.setMailServerHost(mailRes.getString("mail_server_host")); 
		  mailInfo.setMailServerPort(mailRes.getString("mail_server_port")); 
		  mailInfo.setValidate(true); 
		  mailInfo.setUserName(mailRes.getString("user_name")); 
		  mailInfo.setPassword(mailRes.getString("test_password"));//邮箱密码 
		  mailInfo.setFromAddress(mailRes.getString("test_from_address")); 
		  mailInfo.setToAddress(targetAddress); 
		  mailInfo.setSubject(title); 
		  mailInfo.setContent(content); 
	      //这个类主要来发送邮件
		  SimpleMailSender sms = new SimpleMailSender();
	      sms.sendTextMail(mailInfo);//发送文体格式 
	      //sms.sendHtmlMail(mailInfo);//发送html格式
	}
	
	/**
	 * 用于注册时发送校验邮箱有效性的邮件
	 */
	public void sendValidateMail(int userId){
		SysUser user = sysUserMapper.queryById(userId);
		String url = urlRes.getString("msUrl");
		String title = mailRes.getString("title");
		String uuid = UUID.randomUUID().toString();
		
		Date nowTime = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, 24);
		Date expireTime = calendar.getTime();//设置有效期为24小时之内
		
		ForgetPwd record = new ForgetPwd();
		record.setUuid(uuid);
		record.setUserId(userId);
		record.setUseStatus(0);
		record.setExpireTime(expireTime);
		record.setCreatetime(nowTime);
		
		forgetPwdMapper.insert(record);
		String content = user.getNickName()+","+mailRes.getString("content")+url+"/comfirm/"+userId+"/"+uuid+".shtml";
		sendMail(title, content, user.getEmail());
	}
	
	public void sendResetPwdMail(int userId){
		SysUser user = sysUserMapper.queryById(userId);
		String url = urlRes.getString("msUrl");
		String title = mailRes.getString("reset_pwd_title");
		String uuid = UUID.randomUUID().toString();
		
		Date nowTime = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, 24);
		Date expireTime = calendar.getTime();//设置有效期为24小时之内
		
		ForgetPwd record = new ForgetPwd();
		record.setUuid(uuid);
		record.setUserId(userId);
		record.setUseStatus(0);
		record.setExpireTime(expireTime);
		record.setCreatetime(nowTime);
		
		forgetPwdMapper.insert(record);
		String content = user.getNickName()+","+mailRes.getString("reset_pwd_content")+url+"/resetPwd/"+userId+"/"+uuid+".shtml";
		sendMail(title, content, user.getEmail());
	}
	
	/**
	 * 校验email的有效性
	 * @param uuid
	 * @return
	 */
	public boolean validateEmail(String uuid,int uid){
		boolean result = false;
		if(uuid!=null&&!"".equals(uuid)){
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("uuid", uuid);
			param.put("uid", uid);
			ForgetPwd record = forgetPwdMapper.selectByUUID(param);
			if(record!=null&&uuid.equals(record.getUuid())){
				record.setUseStatus(1);//使该uuid不可用
				forgetPwdMapper.updateByPrimaryKeySelective(record);
				result = true;
			}
		}
		return result;
	}
}
